package tech.lq0.dreamaticvoyage.block.machine.fukamizutech.tier2;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import tech.lq0.dreamaticvoyage.block.entity.MoltenBreadEnergyBlockEntity;
import tech.lq0.dreamaticvoyage.capability.ModCapabilities;
import tech.lq0.dreamaticvoyage.capability.uce.IUCEnergyStorage;
import tech.lq0.dreamaticvoyage.init.BlockEntityRegistry;
import tech.lq0.dreamaticvoyage.init.BlockRegistry;

import java.util.List;

@SuppressWarnings("deprecation")
public class MoltenBreadEnergyBlock extends Block implements EntityBlock {

    public MoltenBreadEnergyBlock() {
        super(Properties.copy(BlockRegistry.REINFORCED_FUKAMIZU_MACHINE_CASING.get()));
    }

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) {
        pTooltip.add(Component.translatable("des.dreamaticvoyage.molten_bread_energy_block").withStyle(ChatFormatting.GRAY));
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
        return new MoltenBreadEnergyBlockEntity(pPos, pState);
    }

    @Override
    public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
        if (pLevel.isClientSide) {
            return InteractionResult.SUCCESS;
        } else {
            BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
            if (blockEntity instanceof MoltenBreadEnergyBlockEntity entity) {
                int energy = entity.getCapability(ModCapabilities.UMISU_CURRENT_ENERGY_CAPABILITY).map(IUCEnergyStorage::getEnergyStored).orElse(0);
                int maxEnergy = entity.getCapability(ModCapabilities.UMISU_CURRENT_ENERGY_CAPABILITY).map(IUCEnergyStorage::getMaxEnergyStored).orElse(0);
                pPlayer.displayClientMessage(Component.literal(energy + " / " + maxEnergy + " UCE"), true);
            }
            return InteractionResult.CONSUME;
        }
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
        if (!pLevel.isClientSide) {
            return createTickerHelper(pBlockEntityType, BlockEntityRegistry.MOLTEN_BREAD_ENERGY_BLOCK_ENTITY.get(), MoltenBreadEnergyBlockEntity::serverTick);
        } else {
            return createTickerHelper(pBlockEntityType, BlockEntityRegistry.MOLTEN_BREAD_ENERGY_BLOCK_ENTITY.get(), MoltenBreadEnergyBlockEntity::clientTick);
        }
    }

    @Nullable
    protected static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(BlockEntityType<A> pServerType, BlockEntityType<E> pClientType, BlockEntityTicker<? super E> pTicker) {
        return pClientType == pServerType ? (BlockEntityTicker<A>) pTicker : null;
    }

    @Override
    public ItemStack getCloneItemStack(BlockGetter pLevel, BlockPos pPos, BlockState pState) {
        ItemStack itemstack = super.getCloneItemStack(pLevel, pPos, pState);
        pLevel.getBlockEntity(pPos, BlockEntityRegistry.MOLTEN_BREAD_ENERGY_BLOCK_ENTITY.get()).ifPresent((blockEntity) -> blockEntity.saveToItem(itemstack));
        return itemstack;
    }
}
